<template>
  <el-card>
    <el-form :inline="true" :model="form">
      <el-form-item>
        <el-input v-model="form.order_no" :placeholder="$t('订单号搜索')" clearable />
      </el-form-item>
      <el-form-item>
        <el-select v-model="form.currency" :placeholder="$t('币种')" clearable class="w-28">
          <el-option v-for="item in currencyList" :key="item.key" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="form.status" :placeholder="$t('订单状态')" clearable class="w-36">
          <el-option v-for="item in statusList" :key="item.key" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="form.mer_review_status" :placeholder="$t('商户审核状态')" clearable class="w-36">
          <el-option
            v-for="item in merList"
            :key="item.key"
            :label="item.label"
            :value="item.value"
            ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="form.sys_review_status" :placeholder="$t('平台审核状态')" clearable class="w-36">
          <el-option
            v-for="item in sysList"
            :key="item.key"
            :label="item.label"
            :value="item.value"
            ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="form.timeData" type="datetimerange"
          :range-separator="$t('至')"
          :start-placeholder="$t('开始日期')"
          :end-placeholder="$t('截止日期')" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Search" @click="onSubmit">{{$t('查询')}}</el-button>
        <el-button type="primary" :icon="Plus"
          v-btn="['all', 'web_pay_add']"
          @click="handleClickCreate">{{$t('新增付款')}}</el-button>
        <el-button type="info" @click="onExport">{{ $t("导出") }}</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" v-loading="loading">
      <el-table-column prop="create_time" :label="$t('申请日期')" align="center" :formatter="(row, column, cellValue) => formatterTime(row, column, cellValue, 'day')" />
      <el-table-column prop="create_time" :label="$t('申请时间')" align="center" :formatter="(row, column, cellValue) => formatterTime(row, column, cellValue, 'time')" />
      <el-table-column :label="$t('银行打款时间')" align="center" width="180">
        <template #default="{ row }">
          <p v-for="(item, index) in row.sends">
          {{ formatDates(item.send_pay_time) }}
          </p>
        </template>
      </el-table-column>
      <el-table-column :label="$t('订单金额')" align="right">
        <template #default="{ row }">
            {{ numberToCurrencyNo(row.amount) }}
        </template>
      </el-table-column>
      <el-table-column prop="currency" :label="$t('币种')" align="center"></el-table-column>
      <el-table-column prop="pay_status_name" :label="$t('订单状态')" align="center"></el-table-column>
      <el-table-column prop="mer_review_status" :label="$t('商户审核状态')" align="center">
        <template #default="{ row }">
          {{ row.status === 3 ? '--' : row.mer_review_status_name }}
        </template>
      </el-table-column>
      <el-table-column prop="sys_review_status" :label="$t('平台审核状态')" align="center">
        <template #default="{ row }">
          {{ row.status === 3 ? '--' : row.sys_review_status_name}}
        </template>
      </el-table-column>
      <!--<el-table-column prop="order_no" :label="$t('订单号')" align="center" />-->
      <el-table-column :label="$t('操作')" align="center" fixed="right" width="140">
        <template #default="scope">
          <el-button round :icon="MoreFilled" class="text-[#e54c5f] cursor-pointer"
            v-btn="['all', 'web_pay_view']"
            @click="handleDetails(scope.row)">{{$t('查看')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="mt-5 flex justify-center">
      <el-pagination background
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageable.total"
        :default-page-size="pageable.pageSize"
        :default-current-page="pageable.pageNum"
        :page-sizes="[10, 25, 50, 100]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </div>
  </el-card>
  <DialogForm @closeDialog="closeDialog" @submitForm="dialogSubmit" @handleUserKycChange="handleUserKycChange"
    @handleGoCustomerNew="handleGoCustomerNew" @handleGoCustomerBank="handleGoCustomerBank" :dialogVisible="dialogVisible"
    :currencyList="currencyList" :userKycIdList="userKycIdList" :bankCardIdList="bankCardIdList" :form="dialogForm" />
</template>
<script setup>
import { ref, reactive } from "vue";
import {
  payListApi,
  PayCurrencyApi,
  paySubmitApi,
  payGetUserBanktApi,
  payGetUserListApi,
} from "@/api/account/index";
import { Search, Plus, MoreFilled } from '@element-plus/icons-vue'
import { timestamp, formatterTime, formatDates, numberToCurrencyNo, exportExcel } from "@/utils/utils";
import { useRouter } from "vue-router";
import DialogForm from "./components/DialogForm.vue";
import { ElMessage } from "element-plus";
import { useMerchantStore } from "@/store/modules/merchant";
const merchantStore = useMerchantStore();
const loading = ref(false);
const dialogVisible = ref(false);
const router = useRouter();
const form = reactive({
  order_no: "",
  review_status: "",
  timeData: [],
  currency: "",
  status: ""
});
const dialogFormData = reactive({
  currency: "",
  amount: "",
  user_kyc_id: "",
  bank_card_id: "",
  createtime: ""
});
const dialogForm = reactive({ ...dialogFormData });
const statusList = ref([]);
const currencyList = ref([]); //币种
const userKycIdList = ref([]); //kyc用户
const bankCardIdList = ref([]); //代付用户绑卡信息
const merList = ref([]); //商户审核状态
const sysList = ref([]); //平台审核状态
// 分页
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});
// 表格数据
const tableData = ref([]);
// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    order_no: form.order_no,
    status: form.status,
    start_time: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end_time: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : "",
    mer_review_status: form.mer_review_status,
    sys_review_status: form.sys_review_status,
    currency: form.currency,
  };
};
// 查询
const onSubmit = () => {
  getTbaleData();
};
// 条数
const handleSizeChange = number => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
//获取代付用户绑卡信息
const handleUserKycChange = async (param) => {
  bankCardIdList.value = [];
  const res = await payGetUserBanktApi({ id: param });
  if (res.length) {
    bankCardIdList.value = res;
  }
};
// 打开窗口
const handleClickCreate = async () => {
  dialogVisible.value = true;
  Object.assign(dialogForm, { ...dialogFormData });
  const res = await PayCurrencyApi();
  if (res.length) {
    currencyList.value = res.map((item) => {
      return { value: item, label: item };
    });
  }
  const res1 = await payGetUserListApi();
  if (res1.length) {
    userKycIdList.value = res1.map((item) => {
      return { value: item.id, label: item.real_name, real_name: item.real_name };
    });
  }
};
// 关闭窗口
const closeDialog = () => {
  Object.assign(dialogForm, { ...dialogFormData });
  dialogVisible.value = false;
};
// 提交
const dialogSubmit = async (param) => {
  try {
    const res = await paySubmitApi(param);
    ElMessage({
      showClose: true,
      message: "Success",
      type: "success",
    });
    dialogVisible.value = false;
    getTbaleData();
  } catch (error) {
    return false;
    //    dialogVisible.value = false;
  }
};
// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;
  try {
    // 获取数据
    const res = await payListApi(param());
    // console.log(res);
    loading.value = false;
    tableData.value = res.list;
    pageable.total = res.page.total_count;
    statusList.value = res.extra.filter_status.map((item) => {
      return { value: item.value, label: item.name };
    });
    merList.value = res.extra.filter_mer_review_status.map((item) => {
      return { value: item.value, label: item.name };
    });
    sysList.value = res.extra.filter_sys_review_status.map((item) => {
      return { value: item.value, label: item.name };
    });
  } catch (error) {
    loading.value = false;
  }
};
// 获取币种
const getCurrencyData = async () => {
  const res = await PayCurrencyApi();
  if (res.length) {
    currencyList.value = res.map((item) => {
      return { value: item, label: item };
    });
  }
};
// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1, pagesize: 9999999 });
  const res = await payListApi(data, "blob");
  exportExcel(res, "payment_order");
};
// 查看订单
const handleDetails = ({ ...row }) => {
  router.replace({
    path: "/account/payment/details",
    query: { order_no: row.order_no },
  });
};
const handleGoCustomerNew = () => {
  router.replace({
    path: "/account/customer/new",
  });
};
const handleGoCustomerBank = (param) => {
  router.replace({
    path: "/account/customer/bankCard",
    query: {id: param.kyc_id}
  });
};
Promise.all([getTbaleData(), getCurrencyData()]);
</script>
